package com.cxh.service.impl;

import com.cxh.base.Data2ServiceImpl;
import com.cxh.util.DataSource;
import com.cxh.dao.SysUserDao;
import com.cxh.dao.SysUserRoleDao;
import com.cxh.entity.SysMenu;
import com.cxh.entity.SysUser;
import com.cxh.entity.SysUserRole;
import com.cxh.service.SysUserService;
import com.cxh.util.CommonUtility;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 类说明：
 * @author  ChenXH
 * @date  2018-11-29 11:14:13
 */
@Service("sysUserService")
@DataSource("dataSource2")
public class SysUserServiceImpl extends Data2ServiceImpl<SysUser> implements SysUserService {
    @Autowired
    private SysUserDao dao;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;


    @Override
    public List<SysUser> loadPageList(Map<String, Object> map) {
       return dao.loadPageList(map);
    }


	@Override
	public List<SysMenu> getMenuListByUser(SysUser sysUser) {
		return dao.getMenuListByUser(sysUser);
	}
	
	/**
	 * @类功能说明：更新用户信息，以及用户对应的角色
	 * @作者：Chenxh
	 * @创建时间：2018年12月14日下午1:57:59
	 */
	@Override
	public void updateSysUser(SysUser sysUser, Map<String, Object> jsonMap) {
		//username=测试账号, entityId=, userid=cxh, roleids=1,2}
		Object username = jsonMap.get("username");
		Object userid = jsonMap.get("userid");
		sysUser.setUserid(userid.toString());
		sysUser.setUsername(username.toString());

		//更新用户信息
		dao.updateByPrimaryKey(sysUser);
		//更新用户对应的角色
		Object roleids = jsonMap.get("roleids");
		
		//先将原来的用户拥有的角色全部删除重新加
		sysUserRoleDao.deleteByUserId(userid.toString());
		
		if(roleids.toString().indexOf(",") != -1 ){
			String[] roleIdArray = roleids.toString().split(",");
			for (String roleId : roleIdArray) {
				//重新添加
				SysUserRole sysUserRole = new SysUserRole();
				sysUserRole.setRoleid(roleId);
				sysUserRole.setUserid(userid.toString());
				sysUserRoleDao.addSysUserRole(sysUserRole);
			}
		}else{
			//重新添加
			SysUserRole sysUserRole = new SysUserRole();
			sysUserRole.setRoleid(roleids.toString());
			sysUserRole.setUserid(userid.toString());
			sysUserRoleDao.addSysUserRole(sysUserRole);
		}
	}

	@Override
	public void addSysUser(Map<String, Object> jsonMap) {
		SysUser sysUser = new SysUser();
		sysUser.setPassword("888888");
		sysUser.setUsername(jsonMap.get("username").toString());
		sysUser.setUserid(jsonMap.get("userid").toString());
		//新增用户
		dao.insert(sysUser);
		//新增用户对应的角色
		String roleids = jsonMap.get("roleids").toString();
		if(roleids.indexOf(",") != -1){
			//一个用户对应一个角色
			SysUserRole sysUserRole = new SysUserRole();
			sysUserRole.setRoleid(roleids);
			sysUserRole.setUserid(jsonMap.get("userid").toString());
			sysUserRoleDao.insert(sysUserRole);
		}else{
			String[] roleIdArray = roleids.split(",");
			for (String roleId : roleIdArray) {
				//一个用户对应多个角色
				SysUserRole sysUserRole = new SysUserRole();
				sysUserRole.setRoleid(roleId);
				sysUserRole.setUserid(jsonMap.get("userid").toString());
				sysUserRoleDao.insert(sysUserRole);
			}
		}
	}
	
	
	@Override
	public SysUser getUserByUserId(String userId) {
		return dao.getUserByUserId(userId);
	}
}
